/***************************************** 
 * Desc: UGUI滚动条Inspector
*****************************************/

using Common.UGUI.View;
using System;
using UnityEditor;
using UnityEngine;

namespace Common.UserEditor.UGUIViewSpace {

	[CustomEditor(typeof(UGUIScroller))]
	[CanEditMultipleObjects]
	public class UGUIScrollerInspector : Editor {

		#region 域

		private string _widthString = "0";
		private string _heightString = "0";

        #endregion

        public override void OnInspectorGUI() {

			UnityEngine.Object[] objects = targets;
			int len = objects.Length;
			UGUIScroller[] scrollers = new UGUIScroller[len];
			for (int i = 0; i < len; ++i) {
				scrollers[i] = (UGUIScroller)objects[i];
				scrollers[i].SetColor(Color.clear);
			}

			UGUIPanelInspector.ShowUGUIPanel(scrollers, base.OnInspectorGUI, false);

			ShowIfPanelSizeAffectContent(scrollers);
			ShowIfNeedStick(scrollers);
			ShowSize(scrollers, ref _widthString, ref _heightString);
			ShowProgress(scrollers);
			ShowSensitivity(scrollers);
		}

		#region 自定义子面板

		private static void ShowIfPanelSizeAffectContent(UGUIScroller[] scrollers) {

			UGUIScroller scroller = scrollers[0];

			GUILayout.BeginVertical();

			GUILayout.BeginHorizontal();
			GUILayout.Label("IfPanelSizeAffectContent:", UGUIViewInspector.UGUIViewStatusLabel, GUILayout.ExpandWidth(false));
			bool ifAffect = GUILayout.Toggle(scroller.IfPanelSizeAffectContent, "", UGUIViewInspector.UGUIViewStatusToggle);
			foreach (var oneScroller in scrollers) {
				oneScroller.IfPanelSizeAffectContent = ifAffect;
			}
			GUILayout.EndHorizontal();

			GUILayout.EndVertical();
		}

		private static void ShowIfNeedStick(UGUIScroller[] scrollers) {

			UGUIScroller scroller = scrollers[0];

			GUILayout.BeginVertical();

			GUILayout.BeginHorizontal();
			GUILayout.Label("IfNeedStick:", UGUIViewInspector.UGUIViewStatusLabel, GUILayout.ExpandWidth(false));
			bool ifNeedStick = GUILayout.Toggle(scroller.IfNeedStick, "", UGUIViewInspector.UGUIViewStatusToggle);
			foreach (var oneScroller in scrollers) {
				oneScroller.SetIfNeedStick(ifNeedStick);
			}
			GUILayout.EndHorizontal();
			
			GUILayout.EndVertical();
		}

		private static void ShowSize(UGUIScroller[] scrollers, ref string widthString, ref string heightString) {

			UGUIScroller scroller = scrollers[0];

			//GUILayout.Space(UnityEditorCommonConfig.GUILayoutGapSpace);

			GUILayout.BeginVertical();

			Vector2 sizeNow = scroller.Size;

			GUILayout.BeginHorizontal();
			GUILayout.Label("Scroller Width:", UGUIViewInspector.UGUIViewStatusLabel, GUILayout.ExpandWidth(false));

			widthString = widthString == string.Empty ? widthString : sizeNow.x.ToString();
			//widthString = UnityEditorHelper.TextFieldText(widthString);
			widthString = GUILayout.TextField(widthString, GUILayout.Width(100f));
			GUILayout.EndHorizontal();

			GUILayout.BeginHorizontal();
			GUILayout.Label("Scroller Height:", UGUIViewInspector.UGUIViewStatusLabel, GUILayout.ExpandWidth(false));
			heightString = heightString == string.Empty ? heightString : sizeNow.y.ToString();
			//heightString = UnityEditorHelper.TextFieldText(heightString);
			heightString = GUILayout.TextField(heightString, GUILayout.Width(100f));
			GUILayout.EndHorizontal();

			try {

				float width;
				if (widthString == string.Empty) {
					width = 0;
				} else {
					width = Convert.ToInt32(widthString);
				}

				float height;
				if (heightString == string.Empty) {
					height = 0;
				} else {
					height = Convert.ToInt32(heightString);
				}

				foreach (var oneScroller in scrollers) {
					oneScroller.SetSize(new Vector2(width, height));
				}
			} catch (FormatException e) {

				Debug.LogError("UGUIScroller set size error: " + e.Message);
			}

			GUILayout.EndVertical();
		}

		private static void ShowProgress(UGUIScroller[] scrollers) {

			UGUIScroller scroller = scrollers[0];

			GUILayout.BeginVertical();

			GUILayout.BeginHorizontal();
			GUILayout.Label("Progress:", UGUIViewInspector.UGUIViewStatusLabel, GUILayout.ExpandWidth(false));
			float progress = EditorGUILayout.Slider(scroller.Progress, 0f, 1f, GUILayout.Width(300f));
			foreach (var oneScroller in scrollers) {
				oneScroller.SetProgress(progress);
			}
			GUILayout.EndHorizontal();
			
			GUILayout.EndVertical();
		}

		private static void ShowSensitivity(UGUIScroller[] scrollers) {

			UGUIScroller scroller = scrollers[0];

			GUILayout.BeginVertical();

			GUILayout.BeginHorizontal();
			GUILayout.Label("Sensitivity:", UGUIViewInspector.UGUIViewStatusLabel, GUILayout.ExpandWidth(false));
			float sensitivity = EditorGUILayout.FloatField(scroller.Sensitivity, GUILayout.Width(300f));
			foreach (var oneScroller in scrollers) {
				oneScroller.Sensitivity = sensitivity;
			}
			GUILayout.EndHorizontal();

			GUILayout.EndVertical();
		}

		#endregion
	}
}
